package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务，参数：{}", userId);
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditUserList(Long parentId) {
        log.debug("查询待审核用户业务，参数：{}", parentId);
        //准备一个空数组,用来保存多个审批人对象
        ArrayList<UserVO> userVOList = new ArrayList<>();
        //先将直属领导查出来
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //3.如果有直属领导,并且直属领导有上级领导,则将直属领导的上级领导查出来
        if(auditUser1!= null&&auditUser1.getParentId()!=null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务，参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO == null){
            //用户名错了,查无此人
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            //密码错了
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("用户保存业务，参数：{}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        if (user.getId()==null){//新增
            user.setPassword("123456");
            user.setCreateTime(new Date());
            log.debug("用户对象:{}",user);
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("用户查询业务，参数：{}", userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务，参数：{}", userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("更新用户状态业务，参数：{},{}", userId, status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }


}
